//
// Created by deng on 2022/12/25.
//

#ifndef MOTOR_S_BSP_STMFLASH_H
#define MOTOR_S_BSP_STMFLASH_H

#ifdef __cplusplus
extern "C" {
#endif

#include "main.h"
#include "stm32f4xx.h"

/* FLASH起始地址 */
#define STM32_FLASH_SIZE        0x100000        /* STM32 FLASH 总大小 */
#define STM32_FLASH_BASE        0x08000000      /* STM32 FLASH 起始地址 */

/* FLASH解锁键值 */
#define STM32_FLASH_KEY1               0X45670123
#define STM32_FLASH_KEY2               0XCDEF89AB

/* FLASH 扇区的起始地址 */

#define ADDR_FLASH_SECTOR_0     ((uint32_t )0x08000000)     /* 扇区0起始地址, 16 Kbytes */
#define ADDR_FLASH_SECTOR_1     ((uint32_t )0x08004000)     /* 扇区1起始地址, 16 Kbytes */
#define ADDR_FLASH_SECTOR_2     ((uint32_t )0x08008000)     /* 扇区2起始地址, 16 Kbytes */
#define ADDR_FLASH_SECTOR_3     ((uint32_t )0x0800C000)     /* 扇区3起始地址, 16 Kbytes */
#define ADDR_FLASH_SECTOR_4     ((uint32_t )0x08010000)     /* 扇区4起始地址, 64 Kbytes */
#define ADDR_FLASH_SECTOR_5     ((uint32_t )0x08020000)     /* 扇区5起始地址, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6     ((uint32_t )0x08040000)     /* 扇区6起始地址, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7     ((uint32_t )0x08060000)     /* 扇区7起始地址, 128 Kbytes */
#define ADDR_FLASH_SECTOR_8     ((uint32_t )0x08080000)     /* 扇区8起始地址, 128 Kbytes */
#define ADDR_FLASH_SECTOR_9     ((uint32_t )0x080A0000)     /* 扇区9起始地址, 128 Kbytes */
#define ADDR_FLASH_SECTOR_10    ((uint32_t )0x080C0000)     /* 扇区10起始地址,128 Kbytes */
#define ADDR_FLASH_SECTOR_11    ((uint32_t )0x080E0000)     /* 扇区11起始地址,128 Kbytes */

void test_write(uint32_t waddr, uint32_t wdata);
void stmflash_read(uint32_t raddr, uint32_t *pbuf, uint32_t length);
void stmflash_write(uint32_t waddr, uint32_t *pbuf, uint32_t length);
uint8_t stmflash_get_flash_sector(uint32_t addr);
uint32_t stmflash_read_word(uint32_t faddr);
uint8_t stmflash_write_word(uint32_t faddr, uint32_t data);
uint8_t stmflash_erase_sector(uint32_t saddr);
uint8_t stmflash_wait_done(uint32_t time);
uint8_t  stmflash_get_error_status(void);
void stmflash_lock(void);
void stmflash_unlock(void);
void stmflash_read16(uint32_t raddr, uint16_t *pbuf, uint16_t length);
void stmflash_write16(uint32_t waddr, uint16_t *pbuf, uint16_t length);

#define STM32_FLASH_BASE_ADDR 	(0x08000000) 	//STM32 FLASH的起始地址
#define STM32_FLASH_END_ADDR	(0x08000000+1024*1024)
#define FLASH_WAITETIME  50000

#define FLASH_SAVE_ADDR 		ADDR_FLASH_SECTOR_1	//用户起始地址，暂定为第5个扇区的起始地址
void Flash_EraseSector(uint32_t Address);
void Flash_Read8BitDatas(uint32_t address, uint16_t length, int8_t* data_8);
void Flash_Write8BitDatas(uint32_t address, uint16_t length, int8_t* data_8);
void Flash_Write16BitDatas(uint32_t address, uint16_t length, int16_t* data_16);
HAL_StatusTypeDef Flash_Write32BitDatas(uint32_t address, uint16_t length, int32_t* data_32);
void read_param(uint32_t add_off, int8_t* data_8, uint16_t length);
void write_param(uint32_t add_off, int8_t* data_8, uint16_t length);
void read_param32(uint32_t add_off, int32_t* data_32, uint16_t length);
void write_param32(uint32_t add_off, int32_t* data_32, uint16_t length);
void Flash_Read16BitDatas(uint32_t address, uint16_t length, int16_t* data_16);
void Flash_Read32BitDatas(uint32_t address, uint16_t length, int32_t* data_32);
void clear_param(void);
void stm_flash_unlock(void);
void stm_flash_lock(void);

#ifdef __cplusplus
}


#endif
#endif

